package com.mmg.cosplay.controller;

import com.mmg.cosplay.entity.R;
import com.mmg.cosplay.entity.dto.RoleRankingDTO;
import com.mmg.cosplay.service.RankingListService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Tag(name = "排行榜管理接口")
@RequiredArgsConstructor
@RestController
@RequestMapping("/ranking")
public class RankingListController {

    private final RankingListService rankingListService;

    @Operation(summary = "获取今日角色使用排行榜")
    @GetMapping("/today")
    public ResponseEntity<List<RoleRankingDTO>> getTodayRanking(
            @Parameter(description = "返回数量限制，默认10")
            @RequestParam(value = "limit", defaultValue = "10") int limit) {
        if (limit <= 0 || limit > 100) {
            limit = 10; // 限制范围
        }
        List<RoleRankingDTO> ranking = rankingListService.getTodayRanking(limit);
        return R.success(ranking);
    }

    @Operation(summary = "获取角色今日使用次数")
    @GetMapping("/role/{roleId}/today")
    public ResponseEntity<Long> getRoleTodayUsage(
            @Parameter(description = "角色ID")
            @PathVariable Long roleId) {

        Long usageCount = rankingListService.getRoleTodayUsage(roleId);
        return R.success(usageCount);
    }

    @Operation(summary = "手动清空今日排行榜数据并预热默认数据（仅供测试）")
    @DeleteMapping("/today")
    public ResponseEntity<Void> clearTodayRanking() {
        rankingListService.clearTodayRanking();
        return R.success(null);
    }

    @Operation(summary = "手动预热排行榜默认数据")
    @PostMapping("/preload")
    public ResponseEntity<Void> preloadRanking() {
        rankingListService.manualPreloadRanking();
        return R.success(null);
    }
}
